package com.gitee.wsl.ext.map

import com.gitee.wsl.ext.collection.intersectAll


/**
 * @param src1
 * Original map.
 * @param src2
 * Additional entries to add.
 * @param <K>
 * Key type.
 * @param <V>
 * Value type.
 *
 * @return New map with additional entries.
</V></K> */
fun <K, V> combine( src1: Map<K,V>?, src2: Map<K,V>?): Map<K,V> {
    if (src1 == null) {
        if (src2 == null) return emptyMap()
        return src2
    }
    val map = HashMap(src1)
    if (src2 != null) map.putAll(src2)
    return map
}

 fun <K, T1, T2, R> combine(
     m1: Map<K, T1>,
     m2: Map<K, T2>,
     transform: (K, T1, T2) -> R,
 ): Map<K, R> = intersectAll(m1.keys, m2.keys).associateWith {
    transform(it, m1[it]!!, m2[it]!!)
}

 fun <K, T1, T2, T3, R> combine(
     m1: Map<K, T1>,
     m2: Map<K, T2>,
     m3: Map<K, T3>,
     transform: (K, T1, T2, T3) -> R,
 ): Map<K, R> = intersectAll(m1.keys, m2.keys, m3.keys).associateWith {
    transform(it, m1[it]!!, m2[it]!!, m3[it]!!)
}

 fun <K, T1, T2, T3, T4, R> combine(
     m1: Map<K, T1>,
     m2: Map<K, T2>,
     m3: Map<K, T3>,
     m4: Map<K, T4>,
     transform: (K, T1, T2, T3, T4) -> R,
 ): Map<K, R> = intersectAll(m1.keys, m2.keys, m3.keys, m4.keys).associateWith {
    transform(it, m1[it]!!, m2[it]!!, m3[it]!!, m4[it]!!)
}

 fun <K, T1, T2, T3, T4, T5, R> combine(
     m1: Map<K, T1>,
     m2: Map<K, T2>,
     m3: Map<K, T3>,
     m4: Map<K, T4>,
     m5: Map<K, T5>,
     transform: (K, T1, T2, T3, T4, T5) -> R,
 ): Map<K, R> = intersectAll(m1.keys, m2.keys, m3.keys, m4.keys, m5.keys).associateWith {
    transform(it, m1[it]!!, m2[it]!!, m3[it]!!, m4[it]!!, m5[it]!!)
}
